package dom.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.PreparedStatement;

import dom.util.Connections;

public class Register extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Register() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String psw=request.getParameter("regpsw");
		String account=request.getParameter("regtext");
		int id=Integer.parseInt(request.getParameter("regid")) ;

		com.mysql.jdbc.Connection conn=Connections.getConnection();
		PreparedStatement pstm=null;
		ResultSet rs=null;
		String SQL="insert into manager(id,name,password) values(?,?,?)";
		try {
			pstm =(PreparedStatement) conn.prepareStatement(SQL);
			pstm.setString(2,account);
			pstm.setString(3,psw);
			pstm.setInt(1,id);
			pstm.execute();
//			request.setAttribute("regYes", "璐﹀彿娉ㄥ唽鎴愬姛锛岃鐧婚檰锛�");
			request.getRequestDispatcher("../index.jsp").forward(request, response);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			Connections.closeDB(conn, pstm, rs);
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
